﻿using System;
using System.Collections.Generic;
using Module.Core.LoginSystem.Commons;
using Module.Core.LoginSystem.Commons.Utilities;
using Module.Core.LoginSystem.Components.Commands;
using Module.Core.LoginSystem.Commons.Framwork;
using Telerik.WebControls;

namespace Web.Cms.UserControls
{
    public partial class CommandNavigator : AdminUserControl
    {
        private string _path = string.Empty;
        private CommandInfo _curCommand;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                GetPath();
                LoadSidebar();
            }
        }
        private void LoadSidebar()
        {
            rpMenu.PanelItems.Clear();
            List<CommandInfo> _lstCommands = CommandController.GetCommands(this.CurrentPortalID, 0);
            foreach (CommandInfo _command in _lstCommands)
            {
                if (!_command.IsEnable || !_command.IsVisible) continue;
                if (!CurrentUser.IsSuperUser && !CommandController.UserHasPermision(CurrentUser.UserID, _command.CommandID)) continue;
                PanelItem _item = new PanelItem();
                _item.Text = _command.Title;
                _item.Value = _command.CtrlKey;
                _item.ID = _command.CommandID.ToString();
                _item.ImageCollapsed = "Collapsed.jpg";
                _item.ImageExpanded = "Expanded.jpg";
                _item.ImageHoverCollapsed = "Collapsed_Hover.jpg";
                if (_path.IndexOf("|" + _item.ID + "|") > -1)
                {
                    _item.Expanded = true;
                }
                if (_command.CtrlKey != string.Empty)
                {
                    _item.NavigateUrl = UIProcess.GetCPPage(CurrentPortalID, _command.CtrlKey, null, null);
                }
                if (_curCommand != null && _command.CommandID == _curCommand.CommandID)
                {
                    _item.Selected = true;
                }
                rpMenu.PanelItems.Add(_item);
                LoadSidebarItems(_item, 0);
            }
        }
        private void LoadSidebarItems(PanelItem _curItem, int level)
        {
            int _curCommandId = ConvertUtility.ToInt32(_curItem.ID);
            List<CommandInfo> _lstCommands = CommandController.GetCommands(this.CurrentPortalID, _curCommandId);
            if (level > 0)
            {
                _curItem.ImageCollapsed = "ItemCollapsed.gif";
                _curItem.ImageExpanded = _lstCommands.Count > 0 ? "ItemExpanded.gif" : "ItemSelected.gif";

                _curItem.ItemCollapsedCssClass = "panelbarItem_lvl" + level.ToString();
                _curItem.ItemExpandedCssClass = "panelbarItemExpanded_lvl" + level.ToString();
                _curItem.ItemHoverCollapsedCssClass = "panelbarItemHover_lvl" + level.ToString();
                _curItem.ItemHoverExpandedCssClass = "panelbarItemSelected_lvl" + level.ToString();
                _curItem.ItemSelectedCssClass = "panelbarItemSelected_lvl" + level.ToString();
            }
            foreach (CommandInfo _command in _lstCommands)
            {
                if (!_command.IsEnable || !_command.IsVisible) continue;
                if (!CurrentUser.IsSuperUser && !CommandController.UserHasPermision(CurrentUser.UserID, _command.CommandID)) continue;
                PanelItem _item = new PanelItem();
                _item.Text = _command.Title;
                _item.Value = _command.CtrlKey;
                _item.ID = _command.CommandID.ToString();
                if (_path.IndexOf("|" + _item.ID + "|") > -1)
                {
                    _item.Expanded = true;

                }

                if (_command.CtrlKey != string.Empty)
                {
                    _item.NavigateUrl = UIProcess.GetCPPage(CurrentPortalID, _command.CtrlKey, null, null);
                }
                if (_curCommand != null && _command.CommandID == _curCommand.CommandID)
                {
                    _item.Selected = true;
                }
                _curItem.PanelItems.Add(_item);
                LoadSidebarItems(_item, level + 1);
            }
        }

        private void GetPath()
        {
            _path = "|";
            string _ctrlKey = Request.QueryString["ctrlkey"];
            if (_ctrlKey != null)
            {
                _curCommand = CommandController.GetCommand(_ctrlKey);
                while (_curCommand != null)
                {
                    _path += _curCommand.CommandID + "|";
                    _curCommand = CommandController.GetCommand(_curCommand.ParentID);
                }
            }
        }
    }
}